{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 这个程序用来实现机器学习 HW1 作业5 要求的线性回归，对要求的三十个数据进行拟合。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 初始设置\n",
    "import numpy as np\n",
    "import random\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(30,)\n",
      "(30,)\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXcAAAD8CAYAAACMwORRAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi4zLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvIxREBQAAFFZJREFUeJzt3X+MHHd5x/H3EyeEowUuYLfFlxg7knFJMcLSKdC6Ej+FHYQS16XgtLSEhlpQoKItFrZAFIEqB6IqqlRaalCaQtWEkAbXLUEuYBASJTSmBtIEGUwojc8pCT9MhTAhhKd/3BysL3u3e3uztzPffb+kk29nZnefzG0+O/vMd74bmYkkqSznjLoASVL9DHdJKpDhLkkFMtwlqUCGuyQVyHCXpAIZ7pJUIMNdkgpkuEtSgc4d1ROvXr06169fP6qnl6RW+vznP/+tzFzTa7uRhfv69es5evToqJ5eklopIr7Rz3a2ZSSpQIa7JBXIcJekAhnuklQgw12SCmS4S1KBDHdJKpDhLkkFGtlFTJKa4eCxGa49fJxTp8+wdnKCPds2sWPL1KjL0jIZ7tIYO3hshn233smZhx4GYOb0GfbdeieAAd9ytmWkMXbt4eM/DfY5Zx56mGsPHx9RRaqL4S6NsVOnzyxpudrDcJfG2NrJiSUtV3sY7tIY27NtExPnrTpr2cR5q9izbdOIKlJdPKEqjbG5k6aOlimP4S6NuR1bplob5g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LMtFli/ZYjUu8XGeBGwGDncs3gf8L7NBdQB4E/D2EdW4LjNPRcTFwJGIuBP4vy7bNWE/fgB4RWb+pFpcy36c/1Rdls3/bx/666+Hvp8nIl4OTAPP7lj8iL95Zn6t2/2HXOO/ADdm5oMR8WpmPw09r8/71mEpz7MLuCUzH+5YthL7cWAjCffMfMEyH+IkcFHH7QuBU8zO/zAZEedWR1Rzy2utMSK+GRFPysz7qtC5f5GHeinw4cx8qOOx76t+fTAi/g5446hqrFodZOY9EfEpYAvwTzRoP0bE44CPAG/JzNs7HruW/TjPQq+tbtucjIhzgccze06ln/vWoa/niYgXMPsm+uzMfHBu+QJ/87pDqWeNmfntjpvvBd7Zcd/nzLvvp2qub+55+v177QJe27lghfbjwNralrkD2BizIzoexeyOP5SzZzk+yWyPG+AVQD+fBJbqUPXY/TzHI/p0VZDN9bZ3AP81ihoj4oK5VkZErAa2Anc3aT9Wf98PA+/PzA/NWzeM/dj1tbVI3S8BjlT77BCwqxpNswHYCPxHDTUtucaI2AL8LXB5Zt7fsbzr33xENT6p4+blwJer3w8DL6xqvQB4IWd/8l2xGqs6NzF7YvezHctWaj8ObtRndOf/AL/B7Dvqg8A3gcPV8rXAbR3bvQj4CrPvlG/uWH4xs/9DnQA+BJw/hBqfCHwC+Gr17xOq5dPA+zq2Ww/MAOfMu/8R4E5mw+gfgJ8fRY3Ar1V1fLH69+qm7Ufg5cBDwBc6fp4xzP3Y7bXFbLvn8ur3R1f75ES1jy7uuO+bq/sdBy4b4v8nvWr8ePX/z9w+O9Trbz6CGvcDd1W1fBL45Y77/n61f08ArxxVjdXttwHXzLvfiu3HQX+8QlWSCtTWtowkaRGGuyQVyHCXpAIZ7pJUIMNdkgpkuEtSgQx3SSqQ4S5JBfp/itcakF2kqlUAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 导入数据, 不对数据进行初始化\n",
    "def load_data(filename):\n",
    "    X = []\n",
    "    y = []\n",
    "    with open(filename, 'r') as f:\n",
    "        for line in f:\n",
    "            xi, yi = line.strip('\\n').split('\\t',1) \n",
    "            X.append(float(xi))\n",
    "            y.append(float(yi))\n",
    "    X = np.array(X)\n",
    "    y = np.array(y)\n",
    "    return X, y\n",
    "    \n",
    "filename = 'data'\n",
    "X, y = load_data(filename)\n",
    "print(np.shape(X))\n",
    "print(np.shape(y))\n",
    "# 数据可视化\n",
    "plt.scatter(X, y)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 通过直接计算得到权重"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1.00038681 0.43083834]\n"
     ]
    }
   ],
   "source": [
    "# 线性回归模型1 \n",
    "'''\n",
    "The model is y = w0 + w1*x\n",
    "'''\n",
    "N = X.shape[0]\n",
    "X = np.c_[np.ones(N),X]\n",
    "A = np.dot(X.T,X)\n",
    "B = np.dot(X.T,y)\n",
    "w = np.linalg.solve(A, B)\n",
    "print(w)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 重新导入数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(30,)\n",
      "(30,)\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXcAAAD8CAYAAACMwORRAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi4zLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvIxREBQAAFFZJREFUeJzt3X+MHHd5x/H3EyeEowUuYLfFlxg7knFJMcLSKdC6Ej+FHYQS16XgtLSE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B4IWd/8l2xGqs6NzF7YvezHctWaj8ObtRndOf/AL/B7Dvqg8A3gcPV8rXAbR3bvQj4CrPvlG/uWH4xs/9DnQA+BJw/hBqfCHwC+Gr17xOq5dPA+zq2Ww/MAOfMu/8R4E5mw+gfgJ8fRY3Ar1V1fLH69+qm7Ufg5cBDwBc6fp4xzP3Y7bXFbLvn8ur3R1f75ES1jy7uuO+bq/sdBy4b4v8nvWr8ePX/z9w+O9Trbz6CGvcDd1W1fBL45Y77/n61f08ArxxVjdXttwHXzLvfiu3HQX+8QlWSCtTWtowkaRGGuyQVyHCXpAIZ7pJUIMNdkgpkuEtSgQx3SSqQ4S5JBfp/itcakF2kqlUAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 导入数据, 不对数据进行初始化\n",
    "def load_data(filename):\n",
    "    X = []\n",
    "    y = []\n",
    "    with open(filename, 'r') as f:\n",
    "        for line in f:\n",
    "            xi, yi = line.strip('\\n').split('\\t',1) \n",
    "            X.append(float(xi))\n",
    "            y.append(float(yi))\n",
    "    X = np.array(X)\n",
    "    y = np.array(y)\n",
    "    return X, y\n",
    "    \n",
    "filename = 'data'\n",
    "X, y = load_data(filename)\n",
    "print(np.shape(X))\n",
    "print(np.shape(y))\n",
    "# 数据可视化\n",
    "plt.scatter(X, y)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 1.02956837  0.38614333 -0.14215111]\n"
     ]
    }
   ],
   "source": [
    "# 线性回归模型2\n",
    "'''\n",
    "The model is y = w0 + w1*x + w2*x*x\n",
    "'''\n",
    "N = X.shape[0]\n",
    "X = np.c_[np.ones(N),X,X * X]\n",
    "A = np.dot(X.T,X)\n",
    "B = np.dot(X.T,y)\n",
    "w = np.linalg.solve(A, B)\n",
    "print(w)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "torch",
   "language": "python",
   "name": "torch"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
